
-- Le set up du lua de secteur

local MON_ID_MAP = "MAP40"
local tempsActif = 210
local mallTimer = tempsActif * TICRATE
local tagCooldown = 30 * TICRATE 

local function is_correct_map()
    return (G_BuildMapName(gamemap) == MON_ID_MAP)
end

-- Fonction pour savoir si on est en train de choisir le IT
local function est_en_cooldown_tag()
    local estModeTag = (gametype == GT_TAG or gametype == GT_HIDEANDSEEK)
    return (estModeTag and leveltime < tagCooldown)
end

addHook("MapLoad", function()
    mallTimer = tempsActif * TICRATE
    for player in players.iterate do
        player.derniereZone = mapmusname
    end
end)


-- Fonction pr le rembobinage, on fait une mini mémoire dans le truc

local function changerMusiqueSecurise(nouvelleMusique, player)
    player.derniereZone = nouvelleMusique
    
    -- On bloque UNIQUEMENT si c'est le cooldown du Tag
    if est_en_cooldown_tag() then return end

    if S_MusicName():lower() == nouvelleMusique:lower() then
        return
    end
    S_ChangeMusic(nouvelleMusique, true, player)
end


-- surveillance du décompte

addHook("ThinkFrame", function()
    if not is_correct_map() then return end
    
    if mallTimer > 0 then
        mallTimer = $ - 1
    end

    -- Correction automatique (Désactivée uniquement pendant le cooldown Tag)
    if not est_en_cooldown_tag() and mallTimer > 0 then
        for player in players.iterate do
            if not (player and player.valid) then continue end
            
            local musiqueQuiJoue = S_MusicName():lower()
            local musiqueVoulue = (player.derniereZone or mapmusname):lower()

            if musiqueQuiJoue == mapmusname:lower() and musiqueVoulue ~= mapmusname:lower() then
                S_ChangeMusic(player.derniereZone, true, player)
            end
        end
    end
end)


-- lien avec le linedef 443 


-- 1. ZONE MALL
addHook("LinedefExecute", function(line, mobj)
    if not (mobj and mobj.valid and mobj.player) then return end
    if mallTimer <= 0 then return end

    -- On vérifie si on joue déjà MALL ou NALL
    local musiqueActuelle = S_MusicName():lower()
    if musiqueActuelle == "mall" or musiqueActuelle == "nall" then 
        -- Même si on est déjà dessus, on enregistre pour la mémoire
        mobj.player.derniereZone = musiqueActuelle
        return 
    end

    local musiqueChoisie = "MALL"
    if P_RandomRange(1, 5) == 1 then musiqueChoisie = "NALL" end

    changerMusiqueSecurise(musiqueChoisie, mobj.player)
end, "MALL_MUS")

-- 2. HWA
addHook("LinedefExecute", function(line, mobj)
    if not (mobj and mobj.valid and mobj.player) then return end
    if mallTimer <= 0 then return end
    changerMusiqueSecurise("HWA", mobj.player)
end, "HWA_MUS")

-- 3. GeoRush
addHook("LinedefExecute", function(line, mobj)
    if not (mobj and mobj.valid and mobj.player) then return end
    if mallTimer <= 0 then return end
    changerMusiqueSecurise("GEOR", mobj.player)
end, "GEOR_MUS")

-- 4. Musique principale
addHook("LinedefExecute", function(line, mobj)
    if not (mobj and mobj.valid and mobj.player) then return end
    if mallTimer <= 0 then return end
    changerMusiqueSecurise(mapmusname, mobj.player)
end, "BASE_MUS")